import 'package:trollee/app/components/dialog/app_loading_dialog.dart';
import 'package:trollee/app/components/toast_widget.dart';
import 'package:trollee/app/core/core.dart';
import 'package:trollee/app/core/mixins/http_request_auto_dispose_mixin.dart';
import 'package:trollee/app/data/apis/auth_apis.dart';
import 'package:trollee/app/data/entities/user/user_entity.dart';
import 'package:trollee/app/modules/auth/login/states.dart';
import 'package:trollee/app/routes/app_pages.dart';

class LoginController extends GetxController with HttpRequestAutoDisposeMixin {
  final LoginStates states = LoginStates();

  // 记住账号选择
  RxBool isSelect = false.obs;

  // 验证码
  RxString codeImg = ''.obs;

  // 用户名
  TextEditingController textEditingControllerUserName = TextEditingController();

  // 密码
  TextEditingController textEditingControllerPassword = TextEditingController();

  // 验证码
  TextEditingController textEditingControllerCode = TextEditingController();

  var currentTime = DateTime.now().millisecondsSinceEpoch;

  @override
  void onInit() {
    super.onInit();
    getCode();
  }

  @override
  void onClose() {
    textEditingControllerUserName.dispose();
    textEditingControllerPassword.dispose();
    textEditingControllerCode.dispose();
    super.onClose();
  }

  /// 登录
  void loginDo() async {
    AppLoadingDialog.show();
    Future.delayed(const Duration(seconds: 2)).then((value) {
      //直到首页
      UserManager.saveToken('save-token');
      UserManager.login(UserEntity());
      AppLoadingDialog.close();
      Get.offAllNamed(AppRoutes.home);
    });

    // UserManager.removeToken();

    // 尽量使用 await
    // executeAutoDispose(this) 会在controller关闭时, 取消网络请求
    // final result = await AuthApis.login(
    //   username: textEditingControllerUserName.value.text,
    //   password: textEditingControllerPassword.value.text,
    //   captcha: textEditingControllerCode.value.text,
    //   captchaKey: currentTime.toString(),
    // ).executeAutoDispose(this);
    //
    // result
    //   ..onSuccess((result) {
    //     // 请求成功
    //     AppToast.showSuccess('请求成功');
    //     final UserEntity userEntity = UserEntity.fromJson(result.data as Map<String, dynamic>);
    //     if(isSelect.value) {
    //       UserManager.saveUser(userEntity);
    //     }
    //     UserManager.saveToken(userEntity.accessToken);
    //     UserManager.login(userEntity);
    //     AppLoadingDialog.close();
    //     //直到首页
    //     Get.offAllNamed(AppRoutes.home);
    //   })
    //   ..onError((result) {
    //     AppLoadingDialog.close();
    //     result.showErrorToast();
    //   })
    //   ..onCancel((result) {
    //     AppLogger.log('网络请求被取消了');
    //   });
  }

  /// 获取验证码
  void getCode() async {

    final result = await AuthApis.getCode(
      captchaKey: currentTime,
    ).executeAutoDispose(this);

    result
      ..onSuccess((result) {
        codeImg.value = result.data.toString().split(',')[1];
      })
      ..onError((result) {
        result.showErrorToast();
      })
      ..onCancel((result) {
        AppLogger.log('网络请求被取消了');
      });
  }

  /// 记住账号选择
  void rememberChoose() {
    isSelect.value = !isSelect.value;
  }

  /// 重置账号
  void reset() async {
    textEditingControllerUserName.clear();
    textEditingControllerCode.clear();
    textEditingControllerPassword.clear();
  }

}
